package com.market.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.manage.mapper.AttRecMapper;
import com.market.manage.mapper.UsersMapper;
import com.market.manage.po.*;
import com.market.manage.service.RecordService;
import com.market.manage.vo.ChartVo;
import com.market.manage.vo.DateVo;
import com.market.po.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RecordServiceImpl implements RecordService {
    @Resource
    private AttRecMapper attRecMapper;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat formatInfo = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
    @Resource
    private UsersMapper usersMapper;

    @Override
    public Result getRecord(Long userId) {
        Result result = new Result();
        AttRecExample example = new AttRecExample();
        AttRecExample.Criteria criteria = example.createCriteria();
        criteria.andUseridEqualTo(userId);
        DateVo dateVo = null;
        List<DateVo> list = new ArrayList<>();
        List<AttRec> attRecs = attRecMapper.selectByExample(example);
        Set<String> set = new HashSet<>();
        for (AttRec attRec : attRecs) {
            //循环去重
            String format = this.format.format(attRec.getAttTime());
            set.add(format);
        }
        for (String date : set) {
            dateVo = new DateVo();
            dateVo.setTime(date);
            for (AttRec attRec : attRecs) {
                if (format.format(attRec.getAttTime()).equals(date)) {
                    int hours = attRec.getAttTime().getHours();
                    if ("1".equals(attRec.getStatus())) {
                        if (hours <= 8) {
                            dateVo.setOn(1);
                        } else {
                            //迟到
                            dateVo.setOn(2);
                        }
                    } else if ("2".equals(attRec.getStatus())) {
                        if (hours <= 17) {
                            //早退
                            dateVo.setBack(2);
                        } else {
                            dateVo.setBack(1);
                        }
                    } else if ("3".equals(attRec.getStatus())) {
                        dateVo.setOn(3);
                        dateVo.setBack(3);
                    } else if ("4".equals(attRec.getStatus())) {
                        dateVo.setOn(4);
                        dateVo.setBack(4);
                    }
                }
            }
            list.add(dateVo);
        }
        result.setMessage(list);
        return result;
    }

    @Override
    public Result getData(String time, String username, Integer pageIndex, Integer pageSize) {
        Result result = new Result();
        AttRecExample example = new AttRecExample();
        AttRecExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(time)) {
            try {
                Date parse = format.parse(time);
                Calendar c = Calendar.getInstance();
                c.setTime(parse);
                c.add(Calendar.DAY_OF_MONTH, 1);
                Date today = c.getTime();
                Calendar t = Calendar.getInstance();
                t.setTime(parse);
                t.add(Calendar.DAY_OF_MONTH, 2);
                Date tomorrow = t.getTime();
                criteria.andAttTimeBetween(today, tomorrow);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //设置分页条件
        PageHelper.startPage(pageIndex, pageSize);
        List<AttRec> attRecs = attRecMapper.selectByExample(example);
        Set<String> stringSet = new HashSet<>();
        for (AttRec attRec : attRecs) {
//            String name = usersMapper.selNameById(attRec.getUserid());
//            attRec.setUserName(name);
            stringSet.add(format.format(attRec.getAttTime()));
        }
        List<AttRec> lR = new ArrayList<>();
        AttRec aR = null;
        for (String str : stringSet) {
            aR = new AttRec();
            for (AttRec attRec : attRecs) {
//                aR.setId(attRec.getUserid());
                if (str.equals(format.format(attRec.getAttTime()))) {
                    if ("1".equals(attRec.getStatus())) {
                        aR.setUserid(attRec.getUserid());
                        aR.setUserName(usersMapper.selNameById(attRec.getUserid()));
                        aR.setOnTime(formatInfo.format(attRec.getAttTime()));
                    } else if ("2".equals(attRec.getStatus())) {
                        aR.setUserid(attRec.getUserid());
                        aR.setUserName(usersMapper.selNameById(attRec.getUserid()));
                        aR.setBackTime(formatInfo.format(attRec.getAttTime()));
                    }
                }
            }
            lR.add(aR);
        }
        if (!StringUtils.isEmpty(username)) {
            ArrayList<AttRec> list = new ArrayList<>();
            for (AttRec attRec : lR) {
                if (username.equals(attRec.getUserName())) {
                    list.add(attRec);
                }
            }
            result.setMessage(list);
            result.setTotal(Long.parseLong(list.size() + ""));
            return result;
        }
        PageInfo<AttRec> attRecPageInfo = new PageInfo<>(lR);
        result.setTotal(attRecPageInfo.getTotal());
        result.setMessage(lR);
        return result;
    }

    @Override
    public ChartVo charts() {
        List<String> columns = new ArrayList<>();
        Map<String, Object> rowsMap = null;
        List<Map<String, Object>> rows = new ArrayList<>();
        columns.add("员工");
        columns.add("迟到次数");
        columns.add("早退次数");
        List<Users> users = usersMapper.selAll();
        for (Users u : users) {
            rowsMap = new HashMap<>();
            rowsMap.put("员工", u.getName());
            AttRecExample example = new AttRecExample();
            AttRecExample.Criteria criteria = example.createCriteria();
            criteria.andUseridEqualTo(u.getId());
            List<AttRec> aRs = attRecMapper.selectByExample(example);
            int c1 = 0;
            int c2 = 0;
            for (AttRec a : aRs) {
                if ("1".equals(a.getStatus())) {
                    if (a.getAttTime().getHours() > 8) {
                        c1++;
                    }
                } else if ("2".equals(a.getStatus())) {
                    if (a.getAttTime().getHours() < 17) {
                        c2++;
                    }
                }
            }
            rowsMap.put("迟到次数", c1);
            rowsMap.put("早退次数", c2);
            rows.add(rowsMap);
        }
        ChartVo chartVo = new ChartVo();
        chartVo.setColumns(columns);
        chartVo.setRows(rows);
        return chartVo;
    }
}
